#ifndef TABBAR_H
#define TABBAR_H

#include <QWidget>
#include <QTabBar>
#include <QHBoxLayout>
#include <QPushButton>
#include <QDebug>
#include "bookmarkitem.h"
#include "tabitem.h"

class TabBar : public QWidget
{
	Q_OBJECT
public:
	explicit TabBar(QWidget *parent = 0);
	inline int currentTabIndex(void) { return tabBar->currentIndex(); }
	inline void newTab(BookmarkItem *bookmark) { tabBar->addTab(bookmark->icon(), bookmark->title()); }
	inline void newTab(TabItem *tab) { tabBar->addTab(tab->icon(), tab->title()); }
	inline void changeCurrentTab(BookmarkItem *bookmark)
	{
		tabBar->setTabIcon(tabBar->currentIndex(), bookmark->icon());
		tabBar->setTabText(tabBar->currentIndex(), bookmark->title());
	}
	inline void removeTab(int index)
	{
		tabBar->removeTab(index);
		emit tabRemoved(index);
		emit_selectedTabChanged(tabBar->currentIndex());
	}
signals:
	void tabCreated(int index);
	void tabRemoved(int index);
	void selectedTabChanged(int index);
public slots:
	void changeTabTitle(QString title, int index = -1) { tabBar->setTabText(index != -1 ? index : tabBar->currentIndex(), title); }
	void changeTabIcon(QIcon icon, int index = -1) { tabBar->setTabIcon(index != -1 ? index : tabBar->currentIndex(), icon); }

private slots:
	void emit_selectedTabChanged(int index) { emit selectedTabChanged(index); }
	void emit_tabCreated(void) { emit tabCreated(tabBar->count() - 1); }
protected:
	void mouseReleaseEvent(QMouseEvent *event);
private:
	QTabBar *tabBar;
	QPushButton *buttonAddTab;
	QSpacerItem *horizontalSpacer;
	QHBoxLayout *layout;
};

#endif // TABBAR_H
